﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NHibernate;
using System.Collections;

/// <summary>
/// NHibernateHelper 的摘要描述
/// </summary>
/// 
namespace com.pro2e.console.core.hibernate
{
    public class NHibernateHelper
    {
        public NHibernateHelper()
        {

        }


        public static void Evict(Object obj)
        {
            ISession session = NHibernateSessionManager.Instance.GetSession();
            session.Evict(obj);

        }


        public static void Evict(ICollection ico)
        {
            IEnumerator iemu = ico.GetEnumerator();

            while (iemu.MoveNext())
            {
                Evict(iemu.Current);
            }
        }

   
        public static void Commit()
        {
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            try
            {
                ISession session = NHibernateSessionManager.Instance.GetSession();

                if (session.Transaction.IsActive)
                {
                    session.Flush();
                    session.Transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                ISession session = NHibernateSessionManager.Instance.GetSession();
                log.Error(ex);
                session.Transaction.Rollback();
            }
            finally
            {
                //NHibernateSessionManager.Instance.GetSession().Close()
            }
            
        }

        public static void BeginTransaction()
        {

            ISession session = NHibernateSessionManager.Instance.GetSession();
            session.BeginTransaction();
            
        }

       
    }

}